/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.type;

import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.type.JdbcType;

public final class JavaType {
	/*
	 * This is added to enable basic support for the ARRAY data type - but a
	 * custom type handler is still required
	 */

	private static Map<Class<? extends Object>, JdbcType> codeLookup = new HashMap<Class<? extends Object>, JdbcType>();

	static {
		codeLookup.put(byte.class, JdbcType.TINYINT);
		codeLookup.put(char.class, JdbcType.VARCHAR);
		codeLookup.put(int.class, JdbcType.INTEGER);
		codeLookup.put(long.class, JdbcType.NUMERIC);
		codeLookup.put(float.class, JdbcType.REAL);
		codeLookup.put(double.class, JdbcType.FLOAT);
		codeLookup.put(boolean.class, JdbcType.BIT);
		codeLookup.put(byte[].class, JdbcType.BLOB);

		codeLookup.put(Byte.class, JdbcType.TINYINT);
		codeLookup.put(Character.class, JdbcType.VARCHAR);
		codeLookup.put(Integer.class, JdbcType.INTEGER);
		codeLookup.put(Long.class, JdbcType.NUMERIC);
		codeLookup.put(Float.class, JdbcType.REAL);
		codeLookup.put(Double.class, JdbcType.FLOAT);
		codeLookup.put(Boolean.class, JdbcType.BIT);
		codeLookup.put(Byte[].class, JdbcType.BLOB);

		codeLookup.put(String.class, JdbcType.VARCHAR);
		
		codeLookup.put(Date.class, JdbcType.DATE);
		codeLookup.put(java.sql.Date.class, JdbcType.DATE);
		codeLookup.put(Time.class, JdbcType.TIME);
		codeLookup.put(Timestamp.class, JdbcType.TIMESTAMP);
		
		codeLookup.put(BigDecimal.class, JdbcType.NUMERIC);
		codeLookup.put(Object.class, JdbcType.OTHER);
	}

	JavaType() {
	}

	public static JdbcType forCode(Class<?> code) {
		if (code == null) return null;
		return codeLookup.get(code);
	}
}
